package com.lzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.dao.MemberDao;
import com.lzx.dto.ProductDto;
import com.lzx.dto.RegisterDto;
import com.lzx.dto.UserDto;
import com.lzx.entity.Category;
import com.lzx.entity.Member;
import com.lzx.entity.Pictrue;
import com.lzx.entity.Product;
import com.lzx.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * (Member)表服务实现类
 *
 * @author makejava
 * @since 2021-02-22 19:40:14
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberDao,Member> implements MemberService {
    @Resource
    private MemberDao memberDao;

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Member queryById(Integer id) {
        return this.memberDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Member> queryAllByLimit(int offset, int limit) {
        return this.memberDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @Override
    public Member insert(Member member) {
        this.memberDao.insert(member);
        return member;
    }

    /**
     * 修改数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @Override
    public Member update(Member member) {
        this.memberDao.update(member);
        return this.queryById(member.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.memberDao.deleteById(id) > 0;
    }


    @Override
    public Boolean register(RegisterDto registerDto) {
        Member member = new Member();
        BeanUtil.copyProperties(registerDto,member);
        String pwd = member.getPassword();
        String security = DigestUtil.bcrypt(pwd);
        member.setPassword(security);
        member.setStatus(true);
        int save = memberDao.insert(member);
        return save>0;
    }

    /**
     * 通过用户名查找
     *
     * @param username
     * @return
     */
    @Override
    public Member findByUserName(String username) {
        return null;
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Member login(String username, String password) {
        Member user = memberDao.selectOne(new QueryWrapper<Member>().eq("username",username));
        if(BeanUtil.isNotEmpty(user)){
            boolean checkPass = user.getPassword().equals(password);
            if(checkPass){
                return user;
            }
        }
        return null;
    }

    @Override
    public Boolean changeStatus(Integer id, Boolean status) {
        Member member = memberDao.selectById(id);
        member.setStatus(status);
        int r = memberDao.update(member);
        return r > 0;
    }

    /**
     * 更新Es的用户数据
     */
    @Override
    public void dbToEs() {
        List<Member> memberList = this.list();
        List<UserDto> userDtos = new ArrayList<>();
        memberList.forEach(result->{
            UserDto dto = new UserDto();
            BeanUtil.copyProperties(result,dto);
            userDtos.add(dto);
        });
        restTemplate.save(userDtos);
    }

}
